<?php
/**
 * Template Name: My Venue Bookings
 */

get_header(); ?>

<div class="container">
    <h1><?php the_title() ?></h1>

    <div class="row-fluid">
    </div>

    <div class="row-fluid">
        <div class="span12">
            <div class="card grey">
                <h2 class="title">Bookings</h2>

            <table class="table table-bordered table-striped" style="">
                <thead>
                <tr>
                    <th>Pitch</th>
                    <th>Start</th>
                    <th>End</th>
                    <th>Date</th>
                    <th>Contact</th>
                    <th>Actions</th>
                </tr>
                </thead>
            <tbody>
                <?
                $name = get_user_relation_data('venue','name',get_user_id());
                query_posts(array( 'post_type' => 'pitches','showposts' => 100));
                $bookingCount = 0;
                while (have_posts()) : the_post();
                    if (get_relation_data('venue', 'name') == $name) {
                        $pid = get_field('pid');
                        $the_query2 = new WP_Query(array('post_type' => 'slots', 'showposts' => 100));
                        while ($the_query2->have_posts()) : $the_query2->the_post();
                            if (get_any_relation_data('pitch', 'pid', $the_query2->post->ID) == $pid) {
                                $the_query3 = new WP_Query(array('post_type' => 'bookings', 'showposts' => 100));
                                while ($the_query3->have_posts()) : $the_query3->the_post();
                                    if (get_any_relation_id('slot', $the_query3->post->ID) == $the_query2->post->ID) {
                                        $bookingCount++;
                                        ?>
                                            <tr>
                                                <td><? the_field('pid'); ?></td>
                                                <td><? the_field('start_time', $the_query2->post->ID); ?></td>
                                                <td><? the_field('end_time', $the_query2->post->ID); ?></td>
                                                <td><?

                                                    $date = DateTime::createFromFormat('Ymd', get_field('date', $the_query3->post->ID));
                                                    echo $date->format('l, d M Y');
                                                    ?>
                                                </td>
                                                <td>
                                                    <?
                                                    $customer = get_field('customer_id',$the_query3->post->ID);
                                                    $name = get_field('first_name', 'user_'.$customer);
                                                    //echo $customer;
                                                    echo $name;
                                                    ?>
                                                </td>
                                                <td>
                                                    <a href="#" style="white-space: nowrap;" class="btn btn-primary btn-small">Email &raquo;</a> &nbsp;
                                                    <a href="#" style="white-space: nowrap;" class="btn btn-danger btn-small">Cancel &raquo;</a> &nbsp;
                                                </td>

                                            </tr>
                                        <?
                                    }
                                endwhile;
                            }
                        endwhile;
                    }
                endwhile;
                ?>

            </tbody>
            </table>
            </div>
        </div>
    </div>
</div>


<?php get_footer(); ?>